<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}
if(isset($_GET["field"])){
    if($_GET["field"]==""){
        echo "false";
        exit;
    }
    $id=intval($_GET["id"]);
    $modelid=intval($_GET["modelid"]);
    if(empty($modelid)){
        echo "false";
        exit;        
    }
    $field=trim($_GET["field"]);
    if($id>0){
        $row=$ezhand->select("modelfield",'*','field="'.$field.'" and id<>"'.$id.'" and modelid="'.$modelid.'"');
    }else{
        $row=$ezhand->select("modelfield",'*','field="'.$field.'" and modelid="'.$modelid.'"');
    
    }
    if(empty($row)){
        echo "true";
        exit;        
    }else{
        echo "false";
        exit;        
    }
    
}
if($_POST['sub']!=''){

    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
    
    if(trim($_POST['field'])==""){
        jumpajax('300','字段名不能为空！','');
    }   
    $modelid=intval($_POST['modelid']);
    if(empty($modelid)){
        jumpajax('300','模型ID不能为空！','');
    }           
    $data=array('modelid'=>$_POST['modelid'],'formtype'=>$_POST["formtype"],"field"=>trim($_POST["field"]),"name"=>$_POST["name"],"tips"=>$_POST["tips"],
    "inputsize"=>intval($_POST["inputsize"]),
    "defaultval"=>$_POST["defaultval"],
    "isrequired"=>intval($_POST["isrequired"]),
    "iscompose"=>$_POST["iscompose"],
    "isadmin"=>intval($_POST["isadmin"]),
    "forminfo"=>$_POST["forminfo"],
    
    );
    $formtype=trim($_POST["formtype"]);
    if($formtype_db[$formtype]<>""){
        if($formtype_db[$formtype]=="varchar"){
              $finfo=" VARCHAR( 100 ) NULL ;";
        }elseif($formtype_db[$formtype]=="int"){
             $finfo=" INT( 10 ) NULL DEFAULT '0'";
        }elseif($formtype_db[$formtype]=="text"){
             $finfo=" TEXT NULL ;";
        }
       
        
    }else{
        $finfo="";
    }
    $tablename=$ezhand->select("model",'tablename','id="'.$modelid.'"');
    $tablename=$ezhand->TABLEPRE.$tablename;
    $field=trim($_POST["field"]);
 	if($id==0){
	    //ALTER TABLE `ce_figure` ADD `content` VARCHAR( 100 ) NOT NULL ;
        //ALTER TABLE `ce_figure` ADD `content` INT( 10 ) NULL DEFAULT '0';
        
        
        
        $row=$ezhand->select("modelfield",'id','field="'.$field.'" and modelid="'.$modelid.'"');
        if(!empty($row)){
            
          jumpajax('300','字段‘'.$field.'’已经存在！','');  
        }
        $query=1;
        if(!empty($finfo)){
            if($formtype=="province"){
                $sql="ALTER TABLE `".$tablename."` ADD `".$field."_province` ".$finfo;
                $query=$ezhand->query($sql);
                $sql="ALTER TABLE `".$tablename."` ADD `".$field."_city` ".$finfo;
                $query=$ezhand->query($sql);
                $sql="ALTER TABLE `".$tablename."` ADD `".$field."_district` ".$finfo;
                $query=$ezhand->query($sql);                                                
            }
            $sql="ALTER TABLE `".$tablename."` ADD `".$field."` ".$finfo;
            $query=$ezhand->query($sql);
            
        }
        if($query){
            $id=$ezhand->insert("modelfield",$data);
            
    		jumpajax('200','保存成功','','closeCurrent');
        }else{
            jumpajax('300','字段‘'.$field.'’创建失败！',''); 
        }
	}
	else{
	   //ALTER TABLE `ce_figure` CHANGE `content` `content` INT( 11 ) NOT NULL ; 如果已经有数据，可能会错误
        $row=$ezhand->select("modelfield",'id','field="'.$field.'" and modelid="'.$modelid.'" and id<>'.$id);
        if(!empty($row)){
          jumpajax('300','字段‘'.$field.'’已经存在！','');  
        }
        $drow=$ezhand->select("modelfield",'formtype,issys','id='.$id);
        $oldformtype=$drow["formtype"];
        $isysy=$drow["issys"];
        
        if($oldformtype<>$formtype && $finfo<>"" && $isysy==0){
        $query=1;
        if(!empty($finfo)){
            $sql="ALTER TABLE `".$tablename."` CHANGE `".$field."`  `".$field."` ".$finfo;
           
            $query=$ezhand->query($sql);            
        }
        
        }
        if(empty($finfo)){
            $sql="ALTER TABLE `".$tablename."` DROP `".$field."`;";
            $query=$ezhand->query($sql);                
            
        }
        if($query){
    	    $ezhand->update("modelfield",$data,'id="'.$id.'"');
    		jumpajax('200','修改成功','','closeCurrent');
        }else{
             jumpajax('300','字段‘'.$field.'’修改失败，建议删除后重新创建！',''); 
        }
	}
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
	$modelid=(int)$_GET['modelid'];
    if(empty($modelid)){
        jumpajax('300','模型ID不能为空！','');
    }
    if($id==0){
	    $row=array();
	}
	else{
	    $row=$ezhand->select("modelfield",'*','id="'.$id.'"');
	}
    $row['modelid']=$modelid;
}
?>